﻿function isMousePointInElementRect(x, y, elem) {

    var rect = getElementRect(elem);
    return x > rect.x && x < rect.x + rect.width && y > rect.y && y < rect.y + rect.height;
}

function getElementRect(elem) {

    var x = elem.offsetLeft;
    var y = elem.offsetTop;
    var width = 0;
    var height = 0;

    if (elem.tagName.toLowerCase() == "body"
            || elem.style.position != "relative" || elem.style.position != "absolute")
        return { "x": x, "y": y, "width": width, "height": height };
    else {

        x += getElementRect(elem.parentNode.x);
        y += getElementRect(elem.parentNode.y);
    }

    var width = elem.offsetWidth;
    var height = elem.offsetHeight;

    return { "x": x, "y": y, "width": width, "height": height };
}